﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SpaceFight
{
    class Player : BasicModel
    {
        Matrix rotation = Matrix.Identity;
        float yawAngle = 0;
        float pitchAngle = 0;
        float rollAngle = 0;
        Vector3 direction;
        float movementSpeed = 2000.0f;
        float turningSpeed = 1000.0f;

        float persistentRoll;
        float persistentYaw;
        float persistentPitch;

        float tempRoll = 0;
        float tempYaw = 0;
        float tempPitch = 0;

        public Player(Model model, Vector3 position, float yawAngle, float pitchAngle, float rollAngle, Vector3 direction)
            : base(model)
        {
            Quaternion rot = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), 0f);
            rot.Normalize();
            world = Matrix.CreateScale(1f) * Matrix.CreateFromQuaternion(rot) * Matrix.CreateTranslation(position);
            this.yawAngle = yawAngle;
            this.pitchAngle = pitchAngle;
            this.rollAngle =  rollAngle;
            this.direction = direction;
        }


        public override void Update(GameTime gametime)
        {
            
            ProcessKeyboard(gametime);
            world *= Matrix.CreateTranslation(direction);
            rotation = Matrix.CreateFromYawPitchRoll(persistentYaw, persistentPitch, persistentRoll);
            rollAngle = 0;
            yawAngle = 0;
            pitchAngle = 0;
            base.Update(gametime);
        }

        private void ProcessKeyboard(GameTime gametime)
        {
            KeyboardState keyboardState = Keyboard.GetState();
            
            if (keyboardState.IsKeyDown(Keys.W))
            {
                if (direction.Z < 1.5f)
                    direction.Z += (float)gametime.ElapsedGameTime.TotalMilliseconds / movementSpeed;
                    // direction.Z += 0.1f;
            }
            if (keyboardState.IsKeyDown(Keys.S))
            {
                if(direction.Z > -1.5f)
                    direction.Z -= (float)gametime.ElapsedGameTime.TotalMilliseconds / movementSpeed;
            }

            if (keyboardState.IsKeyDown(Keys.Space))
            {
                if (direction.Z > -1.5f && direction.Z < 0f)    //check bevægelse tilbage
                    direction.Z += (float)gametime.ElapsedGameTime.TotalMilliseconds / movementSpeed*2;

                else if (direction.Z < 1.5f && direction.Z > 0) //check bevægelse fremad
                    direction.Z -= (float)gametime.ElapsedGameTime.TotalMilliseconds / movementSpeed*2;
                
                
                if (direction.Z < 0.1f && direction.Z > 0  || direction.Z > -0.1f && direction.Z < 0)
                    direction = Vector3.Zero;
            }


            float turningSpeed = (float)gametime.ElapsedGameTime.TotalMilliseconds / 80.0f;
            turningSpeed *= 1.6f * 1;

            if (keyboardState.IsKeyDown(Keys.A))
            {

                if (tempRoll > -MathHelper.ToRadians(45 / 2))
                {
                    rollAngle -= MathHelper.ToRadians(turningSpeed);

                }
                yawAngle -= tempRoll / 10;
                //direction.X -= rollAngle;
            }

            if (keyboardState.IsKeyDown(Keys.D))
            {
                if (tempRoll < MathHelper.ToRadians(45 / 2))
                {
                    rollAngle += MathHelper.ToRadians(turningSpeed);

                }
                yawAngle += -tempRoll / 10;
                //pitchAngle += pitchAngle - yawAngle;


                //direction.X += -rollAngle;
            }
            tempRoll += rollAngle;
            tempYaw += yawAngle;
            tempYaw = MathHelper.Clamp(tempYaw, 0, 0.01f);

            persistentRoll += rollAngle;
            persistentYaw += yawAngle;

        }

        public override Matrix GetWorld()
        {
            return  rotation * world;
        }
    }
}